from datetime import datetime
import json,sys,psutil,pyautogui
import os,time,schedule,datetime
import pandas as pd
import webbrowser,cv2,pyscreeze
import requests

def send_wechat_msg(content):
    webhook_url= "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=b20915c0-1822-485c-8a69-ee0a9f594014"
    data = {"msgtype": "text",
    "mentioned_list":["张文强"],
    "text": {"content": content}}
    proxies = {
        "http": None,
        "https": None,
    }
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.40 Safari/537.36 Edg/87.0.664.24'
    }
    requests.packages.urllib3.disable_warnings()
    r = requests.post(url=webhook_url,headers=header, data=json.dumps(data, ensure_ascii=False).encode('utf-8'), verify=False,proxies=proxies)
    return r.text, r.status_code

def getButton(targetImage):
    # 屏幕缩放系数 mac缩放是2 windows一般是1
    screenScale = 1
    # 事先读取按钮截图
    target = cv2.imread(targetImage, cv2.IMREAD_GRAYSCALE)
    # 先截图
    screenshot = pyscreeze.screenshot('my_screenshot.png')
    # 读取图片 灰色会快
    temp = cv2.imread(r'my_screenshot.png', cv2.IMREAD_GRAYSCALE)
    theight, twidth = target.shape[:2]
    tempheight, tempwidth = temp.shape[:2]
    # 先缩放屏幕截图 INTER_LINEAR INTER_AREA
    scaleTemp = cv2.resize(temp, (int(tempwidth / screenScale), int(tempheight / screenScale)))
    stempheight, stempwidth = scaleTemp.shape[:2]
    # 匹配图片
    res = cv2.matchTemplate(scaleTemp, target, cv2.TM_CCOEFF_NORMED)
    mn_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    if (max_val >= 0.9):
        # 计算出中心点
        top_left = max_loc
        bottom_right = (top_left[0] + twidth, top_left[1] + theight)
        tagHalfW = int(twidth / 2)
        tagHalfH = int(theight / 2)
        tagCenterX = top_left[0] + tagHalfW
        tagCenterY = top_left[1] + tagHalfH
        # 左键点击屏幕上的这个位置
        pyautogui.click(tagCenterX, tagCenterY, button='left')
    else:
        print("没找到")

def 知鸟():
    print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "执行任务：播放知鸟视频")
    send_wechat_msg(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")+"执行任务：播放知鸟视频")
    知鸟任务列表=pd.read_excel(os.path.join(sys.path[0],"知鸟.xls"))
    for index,row in 知鸟任务列表.iterrows():
        知鸟任务列表New = pd.read_excel(os.path.join(sys.path[0], "知鸟.xls"))
        if 知鸟任务列表New.loc[index,"完成"]!="Y" and 知鸟任务列表New.loc[index,"日期"]==datetime.date.today():
            webbrowser.open(知鸟任务列表New.loc[index,"地址"])
            time.sleep(10)
            print("开始播放视频")
            getButton(os.path.join(sys.path[0], "确认.png"))
            等待时间片=int((知鸟任务列表New.loc[index,"时长"]*60+180)/180)
            for i in range(等待时间片):
                pyautogui.scroll(100)
                time.sleep(180)
            #超过视频时间，更新视频状态为已完成
            知鸟任务列表New.loc[index,"完成"]="Y"
            播放时长=知鸟任务列表New.loc[index,"时长"]
            send_wechat_msg(f"已自动播放知鸟视频{播放时长}分钟")
            # 知鸟任务列表New.rename(columns={"地址","时长","完成","日期"})
            知鸟任务列表New.to_excel(os.path.join(sys.path[0],"知鸟.xls"),index=False)

知鸟()


